/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
 * secPaver is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

/*
Package config implements the parsing of config file.
*/
package config

import (
	"gitee.com/openeuler/secpaver/common/utils"
)

// GlobalConfig is the global config information parsed by config file
type GlobalConfig struct {
	Connect    ConnectInfo    `json:"connect"`
	Repository RepositoryInfo `json:"repository"`
	Log        LogInfo        `json:"log"`
}

// ConnectInfo is the net connection config information parsed by config file
type ConnectInfo struct {
	Grpc Grpc `json:"grpc"`
}

// Grpc is grpc config
type Grpc struct {
	Socket string `json:"socket"`
}

// RepositoryInfo is the repository paths parsed by config file
type RepositoryInfo struct {
	PolicyRoot string `json:"policies"`
}

// LogInfo is the log information parsed by config file
type LogInfo struct {
	FilePath    string `json:"path"`
	Level       string `json:"level"`
	MaxFileSize int    `json:"maxFileSize"`
	MaxFileNum  int    `json:"maxFileNum"`
	MaxFileAge  int    `json:"maxFileAge"`
}

// Parse function parse the Pavd global config file
func Parse(path string) (*GlobalConfig, error) {
	config := &GlobalConfig{}
	err := utils.UnmarshalJSONFile(path, config)
	if err != nil {
		return nil, err
	}

	return config, nil
}
